/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package deadlocksim.gui;
import deadlocksim.*;
import javax.swing.*;
import java.awt.event.*;
/**
 *
 * @author CCSCHMID
 */
public class ResourceTypeButton extends JButton
{
    //original declarations
    private DeadlockSim mySim;
    private int resourceTypeID;
    //start popupmenu stuff
    private JPopupMenu resourcePopupMenu;
    private JMenuItem createResourceMenuItem;
    private JMenuItem freeResourceMenuItem;
    private JMenuItem deleteResourceMenuItem;
    private JMenuItem deleteResourceTypeMenuItem;
    private JMenuItem cancelResourceMenuItem;
    private ActionListener resourceActionListener;
    private MouseListener resourcePopupListener;
    //end popupmenu stuff
            
    //original methods
    public ResourceTypeButton(int typeID, DeadlockSim sim)
    {
        //orignal calls
        super();
        mySim = sim;
        setID(typeID);
        //start popupmenu calls
        initResourcePopupMenu();
        this.addMouseListener(resourcePopupListener);
        //end popupmenu calls
    }
    
    public void setID(int typeID)
    {
        resourceTypeID = typeID;
        setText(getDisplayText());
    }
    public int getResourceTypeID()
    {
        return resourceTypeID;
    }
    public String getDisplayText()
    {
        String baseString = "R"+ resourceTypeID;
        Integer numAvail = mySim.getResourceManager().getNumAvail(resourceTypeID);
        if(numAvail == null)
            return baseString;
        else
            return  baseString + " Avail: " + numAvail;
    }
    
    //start popupmenu methods
    public void initResourcePopupMenu()
    {
        resourcePopupMenu = new JPopupMenu("Resource Menu");
                
        createResourceMenuItem = new JMenuItem("Create 1 resource of type R" + 
                resourceTypeID);
        createResourceMenuItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                //perform free resource operation
                mySim.createResource(resourceTypeID);
                if(mySim.isRunning())
                {
                    //message already in createResource method
                    //mySim.sendMessage("Created 1 resource of type R" + resourceTypeID +"!",false);
                    mySim.startClock();                    
                }
            }
        });
        resourcePopupMenu.add(createResourceMenuItem);
        
        final String actionText = "Delete 1 available resource of type R" +
                resourceTypeID + " from simulator";
        deleteResourceMenuItem = new JMenuItem(actionText);
        deleteResourceMenuItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                
                //perform delete resource operation
                mySim.getResourceManager().deleteFreeResource(resourceTypeID);
                mySim.refreshDisplay();
                if(mySim.isRunning())
                    mySim.startClock();
            }
        });
        resourcePopupMenu.add(deleteResourceMenuItem);
        
        freeResourceMenuItem = new JMenuItem("Free ALL resources of type R" + 
                resourceTypeID);
        freeResourceMenuItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                //perform free resource operation
                mySim.getResourceManager().takeAllOfType(resourceTypeID);
                
                mySim.refreshDisplay();
                if(mySim.isRunning())
                    mySim.startClock();
            }
        });
        resourcePopupMenu.add(freeResourceMenuItem);
        
        final String removeAllActionText = "REMOVE ALL resources of type R" +
                resourceTypeID + " from simulator";
        deleteResourceTypeMenuItem = new JMenuItem(removeAllActionText);
        deleteResourceTypeMenuItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                int response = JOptionPane.showConfirmDialog(resourcePopupMenu, 
                        "Are you sure you want to " + removeAllActionText + "?");
                if(response == JOptionPane.OK_OPTION)
                {
                    //perform delete resource operation
                    mySim.destroyResourceType(resourceTypeID);
                    
                }
                if(mySim.isRunning())
                    mySim.startClock();
            }
        });
        resourcePopupMenu.add(deleteResourceTypeMenuItem);
        
        cancelResourceMenuItem = new JMenuItem("Cancel");
        cancelResourceMenuItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                //cancel the menu, nothing to do
                //System.out.println("Cancel");
                if(mySim.isRunning())
                    mySim.startClock();
            }
        });
        resourcePopupMenu.add(cancelResourceMenuItem);
        
        resourcePopupListener = new PopupListener(resourcePopupMenu);
    }
    public void refreshDisplay()
    {
        setText(getDisplayText());
    }
    
    public boolean equals(Object o)
    {
        if(o instanceof ResourceTypeButton)
        {
            ResourceTypeButton otherB = (ResourceTypeButton)o;
            if(this.resourceTypeID == otherB.resourceTypeID)
            {
                return true;
            }
            return false;
        }
        else
        {
            return false;
        }
    }
        
    class PopupListener extends MouseAdapter {
        JPopupMenu popup;
        
        PopupListener(JPopupMenu popupMenu) {
            popup = popupMenu;
        }
        @Override
        public void mousePressed(MouseEvent e) {
            maybeShowPopup(e);
        }
        @Override
        public void mouseReleased(MouseEvent e) {
            maybeShowPopup(e);
        }
        private void maybeShowPopup(MouseEvent e) {
            if (!e.isPopupTrigger()) {
                popup.show(e.getComponent(), e.getX(), e.getY());
                if(mySim.isRunning())
                    mySim.stopClock();
            }
        }
    }
    //end popupmenu methods
    

}
